$(function() {
    Contract.setInputWidth();
    Contract.setInputWidth();
    Contract.setInputWidth();
    Contract.setInputWidth();
    Contract.setInputWidth();
    Contract.postContractId();
});

var Contract = {

    /*设置input输入宽度*/
    setInputWidth: function() {
        // console.log(app.url.api_base);
        $(".common-style li").each(function() {
            var baseWidth = $(this).children('i').length ? $(this).children('i').width() + 21 : 8,
                oWidth = $(this).width() - $(this).children('label').width() - baseWidth + 'px';
            $(this).children('input').css({ 'width': oWidth });
        });
    },

    /*复选框按钮点击事件*/
    selectCheckBox: function() {
        $('.iconfont').each(function(index) {
            if ($(this).siblings('input').val()) {
                $(this).addClass('icon-duihao');
            }
        });
    },


    /*提交token参数 获取合同内容*/
    postContractId: function() {
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: app.url.api_base+'contracts/print',
            data: {
                'token': app.getParameterByName('token')
            },
            success: function(res) {

                if (res.code == 0) {
                    var data = res.data.contractResponse;
                    $('.contractNo').html('合同号：' + '' + data.contractNo + '');
                    $('.name input').val(data.studentName);
                    $('.sex input').val(data.gender);
                    $('.birthDay input').val(data.birthDay);
                    $('.tel input').val(data.contractMobile);
                    $('.course input').val(data.courseName);
                    $('.lessonHour input').val(data.lessonHour +'课时');
                    $('.courseFee input').val(data.courseFee +'元');
                    $('.cashPrice').val(data.cashPrice > 0 ? data.cashPrice + '元': '');
                    $('.posPrice').val(data.posPrice > 0 ? data.posPrice + '元': '');
                    $('.wx').val(data.wechatPrice > 0 ? data.wechatPrice + '元': '');
                    $('.zfb').val(data.zfbPrice > 0 ? data.zfbPrice + '元': '');
                    $('.other').val(data.otherPrice > 0 ? data.otherPrice + '元': '');

                    Contract.selectCheckBox();
                } else {
                    Prompt.show('获取信息失败，请稍候重试');
                }

            }
        });
    }

};

//封装alert
 var Prompt = { 
    LMark: null, 
    show: function(meo, title, _ok_fn, _cancel_fn) {
        var Lbody = document.body;
        var LO = document.createElement("div");
        LO.style.background = "rgba(0,0,0,.2)";
        LO.style.position = "fixed";
        LO.style.zIndex = 1000000;
        LO.style.top = "0";
        LO.style.left = "0";
        LO.style.width = "100%";
        LO.style.height = "100%";
        LO.setAttribute("id", "LMark");
        this.LMark = LO;
        var La = document.createElement("div");
        La.style.position = "absolute";
        La.style.top = "50%";
        La.style.left = "50%";
        La.style.background = "#fff";
        La.style.borderRadius = "4px";
        La.style.padding = "15px 20px 20px";

        La.style.width = "350px";
        La.style.marginLeft = "-175px";
        La.style.marginTop = "-150px";
        La.style.textAlign = "center";
        LO.appendChild(La);
        var Lh = document.createElement("h1");
        Lh.style.padding = "10px 0 0px ";
        Lh.style.margin = "0px";
        Lh.style.color = "#333";
        Lh.style.fontWeight = "normal";
        Lh.style.fontSize = "18px";
        if (typeof title != "undefined") {
            if (typeof title == "function") { Lh.innerHTML = "" } else { Lh.innerHTML = title } } else { Lh.innerHTML = "" }
        if (Lh.innerHTML != "") { La.appendChild(Lh) }
        var Lp = document.createElement("p");
        Lp.style.padding = "0";
        Lp.style.lineHeight = "25px";
        Lp.style.margin = "15px";
        Lp.style.fontSize = "18px";
        Lp.style.color = "#666";
        Lp.innerHTML = meo;
        La.appendChild(Lp);
        var Lc = document.createElement("span");
        Lc.style.display = "inline-block";
        Lc.style.padding = "5px 30px";
        Lc.style.margin = "0 15px";
        Lc.innerHTML = "取消";
        Lc.setAttribute("id", "Lback");
        Lc.setAttribute("class", "btn btn-default ");
        if (typeof title != "undefined") {
            if (typeof title == "function") { this.cancel_event.event = _ok_fn } else { this.cancel_event.event = _cancel_fn }
            if (typeof this.cancel_event.event == "function") { La.appendChild(Lc) } }
        Lc.setAttribute("onclick", "Prompt.cancel_event.back()");
        var Lb = document.createElement("span");
        Lb.style.display = "inline-block";
        Lb.style.padding = "5px 30px";
        Lb.style.margin = "0 15px";
         
        Lb.innerHTML = "确定";
        Lb.setAttribute("id", "LBtn");
        Lb.setAttribute("class", "btn btn-primary");
        La.appendChild(Lb);
        if (typeof title != "undefined") {
            if (typeof title == "function") { this.ok_event.event = title } else { this.ok_event.event = _ok_fn } }
        Lb.setAttribute("onclick", "Prompt.ok_event.go()");
        Lbody.appendChild(LO) 
    },
    ok_event: { 
        event: function() {}, 
        go: function() {
         	this.event();
         	var Lbody = document.body;
         	var LO = document.getElementById("LMark");
         	Lbody.removeChild(LO)
        }
    },
    cancel_event: { 
        event: function() {}, 
        back: function() { 
            this.event();
            var Lbody = document.body;
            var LO = document.getElementById("LMark");
            Lbody.removeChild(LO)
         } 
    } 
};